package pers.kw.config.oauth;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * token增强配置
 */
public class CustomTokenEnhancer implements TokenEnhancer {

    private static final Logger log = LoggerFactory.getLogger(CustomTokenEnhancer.class);

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
        log.info("token增强处理start...");
        MyParamValue current = MyParamValueThreadLocal.getCurrent();
        log.info("获取自定义参数信息：{}", JSON.toJSONString(current));
        log.info("OAuth2AccessToken: {}", JSON.toJSONString(oAuth2AccessToken));
        log.info("OAuth2Authentication: {}", JSON.toJSONString(oAuth2Authentication));

        Map<String, Object> additionalInfo = new HashMap<>();
        additionalInfo.put("user_type", "1");
        //todo 通过调用接口查询，封装所需要的参数
        ((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(additionalInfo);

        log.info("token增强处理end...");
        return oAuth2AccessToken;
    }
}
